package com.yiwei.ydd.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.OilCardAddActivity;
import com.yiwei.ydd.adapter.OilChangeAdapter;
import com.yiwei.ydd.api.model.OilcardListModel;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by xsooy-pc on 2017/4/24.
 */

public class OilChangeDialog extends Dialog {


    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_add_new)
    RelativeLayout layoutAddNew;
    @BindView(R.id.img_icon)
    ImageView imgIcon;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Context context;
    private OilChangeAdapter adapter;
    private String select_id;
    private String select_ctype;
    private String select_card_number;
    private boolean isFromOrder = false;
    private String order_no;

    public OilChangeDialog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public OilChangeDialog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    public void setIsFromOrder(boolean isFromOrder, String order_no) {
        this.isFromOrder = isFromOrder;
        this.order_no = order_no;
    }

    public String getOrderNo() {
        return order_no;
    }

    private void initView() {
        setContentView(R.layout.dialog_change_oil);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = (int) (Util.getDisplayMetrics(context).widthPixels);
        mWindow.setAttributes(lp);

        adapter = new OilChangeAdapter(context);
        adapter.setOnChooseListener(new OilChangeAdapter.OnChooseListener() {
            @Override
            public void onClick(String id, String ctype, String card_number) {
                select_id = id;
                select_ctype = ctype;
                select_card_number = card_number;
            }
        });
        list.setAdapter(adapter);
    }


    public void setData(List<OilcardListModel.DatasBean> data) {
        adapter.update(data);
    }


    private DialogDefaultClickListener listener;

    public void setDialogClickListener(DialogDefaultClickListener listener) {
        this.listener = listener;
    }

//    @OnClick(R.id.layout_add_new)
//    public void onViewClicked() {
//
//    }

    @OnClick({R.id.layout_add_new, R.id.btn_submit, R.id.btn_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_close:
                dismiss();
                break;
            case R.id.layout_add_new:
                if (isFromOrder) {
                    Intent intent2 = new Intent(getContext(), OilCardAddActivity.class);
                    intent2.putExtra("add_order", true);
                    intent2.putExtra("order_no", order_no);
                    getContext().startActivity(intent2);
                    dismiss();
                } else {
                    Util.startActivity(context, OilCardAddActivity.class);
                    dismiss();
                }

                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(select_id)) {
                    ToastUtil.makeText(context, "请选择油卡");
                    return;
                }
                if (listener != null) {
                    listener.confirm(select_id, select_ctype, select_card_number);
                }
                dismiss();
                break;
        }
    }


    public interface DialogDefaultClickListener {
        public abstract void confirm(String id, String ctype, String card_number);

        public abstract void cancel();

    }
}
